package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.core.ClientHandler;
import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {

    private static File ARTICLE_DIR = new File("./articles");

//    private static File staticDir;

    static{
        if(!ARTICLE_DIR.exists()){
            ARTICLE_DIR.mkdirs();
        }

//        try {
//            staticDir = new File(
//                    ClientHandler.class.getClassLoader().getResource(
//                            "./static"
//                    ).toURI()
//            );
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始发送文章");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title+","+author+","+content);

        if (title==null || author==null || content==null){
//            File file = new File(staticDir,"/myweb/article_fail.html");
//            response.setContentFile(file);
            response.sendRedirest("/myweb/article_fail.html");
            return;
        }
        Article article = new Article(title,author,content);
        File articleFile = new File(ARTICLE_DIR,title+".obj");
        if (!articleFile.exists()){
            try(
                    FileOutputStream fos = new FileOutputStream(articleFile);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
            ){
                oos.writeObject(article);
//                File file = new File(staticDir,"/myweb/article_success.html");
//                response.setContentFile(file);
                response.sendRedirest("/myweb/article_success.html");
            }catch (IOException e){

            }
            return;
        }
//        File file = new File(staticDir,"/myweb/article_fail.html");
//        response.setContentFile(file);

        response.sendRedirest("/myweb/article_fail.html");


        System.out.println("发送文章结束");

    }
    @RequestMapping("/myweb/showArticle")
    public void showArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("文章列表开始了");
        List<Article> list = new ArrayList<>();
        File dir = new File("./articles");
        File[] files = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for(File file : files){
            try(
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    ) {
                Article article = (Article)ois.readObject();
                list.add(article);
            }catch (IOException | ClassNotFoundException e){

            }
        }
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("<td>内容</td>");
        pw.println("</tr>");

        for (Article article : list) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("<td>" + article.getContent() + "</td>");
            pw.println("</tr>");
        }

        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");

        System.out.println("文章列表结束了");
    }
}
